package com.istock.monitor.agent.model;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

public class AOPHandler implements MethodInterceptor{

	public Object around(ProceedingJoinPoint point) throws Throwable {
		StringBuffer sb = new StringBuffer();
		try {
			MethodSignature signature = (MethodSignature)point.getSignature();
			Class targetClass = point.getTarget().getClass();
//			Method method = signature.getMethod();
			sb.append("***").append(targetClass.getName()).append("---").append(signature.toLongString());
			Object result = point.proceed();
			sb.append("====").append(result);
			System.out.println(sb.toString());
			return result;
		}catch(Throwable t) {
			throw t;
		}
	}

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		StringBuffer sb = new StringBuffer();
		try {
//			MethodSignature signature = (MethodSignature)point.getSignature();
			Class targetClass = invocation.getThis().getClass();
//			Method method = signature.getMethod();
			sb.append("***").append(targetClass.getName()).append("---").append(invocation.getMethod().toGenericString());
			Object result = invocation.proceed();
			sb.append("====").append(result);
			System.out.println(sb.toString());
			return result;
		}catch(Throwable t) {
			throw t;
		}
	}
}
